package lg.minecraft.plugin.library.command;

import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.entity.Player;

/**
 * @author langle__
 * @version 1.0
 */
public enum CommandType {
    PLAYER(CommandType::isPlayer),
    CONSOLE(CommandType::isConsole),
    COMMAND_BLOCK(CommandType::isCommandBlock),
    Remote(CommandType::isRemote),
    SERVER(CommandType::isServer),
    ALL(sender -> true);

    private final SenderPredicate predicate;

    CommandType(SenderPredicate predicate) {
        this.predicate = predicate;
    }

    public boolean test(CommandSender sender) {
        return predicate.test(sender);
    }

    private static boolean isPlayer(CommandSender sender) {
        return sender instanceof Player;
    }

    private static boolean isConsole(CommandSender sender) {
        return sender instanceof ConsoleCommandSender;
    }

    private static boolean isCommandBlock(CommandSender sender) {
        return sender instanceof BlockCommandSender;
    }

    private static boolean isRemote(CommandSender sender) {
        return sender instanceof RemoteConsoleCommandSender;
    }

    private static boolean isServer(CommandSender sender) {
        return isConsole(sender) || isCommandBlock(sender) || isRemote(sender);
    }

    @FunctionalInterface
    private interface SenderPredicate {
        boolean test(CommandSender sender);
    }

}
